﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using MP.LayerHelper.MP_OBS.BLL;
using MP.LayerHelper.MP_OBS.DAL.EntityClasses;
using MP.LayerHelper.MP_OBS.DAL.HelperClasses;

using MP.Core.Config;
using MP.Core.Library;

public partial class Member_Control_ManagerRoleDepart : MP.Core.Authentication.Control.AuthenBase //System.Web.UI.UserControl
{
    //MpModuleDepartService smodule = (MpModuleDepartService)WebUtility.GetWebService(EnumServices.MP_OBS, EnumServicesMPMember.MpModuleDepartPath, typeof(MpModuleDepartService), EnumServicesMPMember.MpModuleDepartClass);
    //MpRoleDepartService srole = (MpRoleDepartService)WebUtility.GetWebService(EnumServices.MP_OBS, EnumServicesMPMember.MpRoleDepartPath, typeof(MpRoleDepartService), EnumServicesMPMember.MpRoleDepartClass);
    //HrDepartmentService sdepart = (HrDepartmentService)WebUtility.GetWebService(EnumServices.MP_OBS, EnumServicesMPMember.HrDepartmentPath, typeof(HrDepartmentService), EnumServicesMPMember.HrDepartmentClass);

    public string DepartCode
    {
        get { return Request["depart"]; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        InitAuthen(EnumModules.SUB_ROLE);

        if (!IsPostBack)
        {
            if (!IsAdd())
            {
                RedirectLogin();
            }

            loadRole();
            loadModule();
        }
    }

    void loadRole()
    {
        MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetByDepartCode(DepartCode);
        if (depart != null)
        {
            lblDepartName.Text = depart.DepartName;

            DataSet dst = MbRoleDepartManager.CreateInstant().GetRoleDepart(depart.Id);
            repRole.DataSource = dst;
            repRole.DataBind();
        }
    }

    void loadModule()
    {
        MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetByDepartCode(DepartCode);
        if (depart != null)
        {
            DataSet dst = MbModuleDepartManager.CreateInstant().GetModuleDepart(depart.Id);
            repModule.DataSource = dst;
            repModule.DataBind();
        }
    }

    protected void btnRole_Click(object sender, EventArgs e)
    {
        if (repRole.Items != null && repRole.Items.Count >0)
        {
            MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetByDepartCode(DepartCode);
            if (depart != null)
            {
                for (int i = 0; i < repRole.Items.Count; i++)
                {
                    HiddenField hid = (HiddenField)repRole.Items[i].FindControl("hid");
                    CheckBox chkRole = (CheckBox)repRole.Items[i].FindControl("chkRole");

                    MbRoleDepartManager.CreateInstant().UpdateRoleDepart(depart.Id, new Guid(hid.Value), chkRole.Checked);
                }
            }

            Response.Redirect(Request.RawUrl + "#role");
        }
    }

    protected void btnModule_Click(object sender, EventArgs e)
    {
        if (repModule.Items != null && repModule.Items.Count > 0)
        {
            MaDepartmentEntity depart = MaDepartmentManager.CreateInstant().GetByDepartCode(DepartCode);
            if (depart != null)
            {
                for (int i = 0; i < repModule.Items.Count; i++)
                {
                    HiddenField hid = (HiddenField)repModule.Items[i].FindControl("hid");
                    CheckBox chkModule = (CheckBox)repModule.Items[i].FindControl("chkModule");

                    MbModuleDepartManager.CreateInstant().UpdateModuleDepart(depart.Id, hid.Value, chkModule.Checked);
                }
            }

            Response.Redirect(Request.RawUrl + "#module");
        }
    }
}
